package com.wsestadio.dao.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class DAOBase {

	protected EntityManager em;

	public DAOBase(EntityManager em) {
		this.em = em;
	}

	protected List getQuery(String sqlQuery, Map<String, Object> map)
			throws Exception {
		Query q = em.createQuery(sqlQuery);
		for (Entry<String, Object> entry : map.entrySet()) {
			System.out.println(entry.getKey() + "/" + entry.getValue());
			q.setParameter(entry.getKey(), entry.getValue());
		}
		return q.getResultList();
	}

	protected Object getSingleQuery(String sqlQuery, Map<String, Object> map)
			throws Exception {
		Query q = em.createQuery(sqlQuery);
		for (Entry<String, Object> entry : map.entrySet()) {
			System.out.println(entry.getKey() + "/" + entry.getValue());
			q.setParameter(entry.getKey(), entry.getValue());
		}
		return q.getSingleResult();
	}

	public static Map<String, Object> getNewMap() {
		return new HashMap<String, Object>();
	}

}
